// 引入axios组件
import axios from 'axios';
import qs from 'qs'; // 处理数据  qs.stringify(params)
// 数据删除之后  需要重新请求页面  不建议用路由  用监听自动加载~
import { checkRule } from '@/assets/js/public';
// 引入公共js文件
export default {
  data() {
    return {
      modal1: false,
      search: '', // 搜索内容
      param: '',
      putpage: '', // 存储当前页码
      columns7: [
        {
          type: 'selection',
          width: 60,
          align: 'center',
        },
        {
          title: '用户名',
          key: 'username',
          render: (h, params) => h('div', [
            h('strong', params.row.username),
          ]),
        },
        {
          title: '角色',
          key: 'rolename',
        },
        {
          title: '登录ip',
          key: 'ip',
        },
        {
          title: '登录时间',
          key: 'logtime',
        },
        {
          title: '操作选项',
          key: 'action',
          width: 150,
          align: 'center',
          render: (h, params) => h('div', [
            h(
              'Button',
              {
                props: {
                  type: 'info',
                  size: 'small',
                },
                style: {
                  marginRight: '5px',
                },
                on: {
                  click: () => {
                    this.show(params.row.id);
                    // this.show(params);
                  },
                },
              },
              '编辑',
            ),
            h(
              'Button',
              {
                props: {
                  type: 'error',
                  size: 'small',
                },
                style: {
                  marginRight: '5px',
                },
                on: {
                  click: () => {
                    this.remove(params);
                  },
                },
              },
              '删除',
            ),

          ]),
        },
      ],
    };
  },
  created() {
    const self = this;
    this.$Spin.show();
    setTimeout(() => {
      this.$Spin.hide();
    }, 500);
    const spage = this.$route.query.page;
    const asearch = this.$route.query.search;
    axios({
      method: 'post',
      url: '/admin/admin/adminList',
      data: { page: spage, search: asearch },
    })
      .then((response) => {
        checkRule(response, self);// 检查是否有权限
        self.putpage = spage;
        self.$store.commit('adminlistinfo', response.data);
        console.log(response);
      })
      .catch((response) => {
        console.log(response);
      });
  },
  // 用来获取VUEX中的文件
  computed: {
    data6() {
      return this.$store.state.admin.admininfoshow;
    },
    page() {
      console.log(this.$store.state.admin.admininfo);
      return this.$store.state.admin.admininfo;
    },
  },
  methods: {
    // 分页的钩子函数，当跳页的时候重新请求（将页码发给后台）
    getpage(page) {
      const self = this;
      if (page == undefined) {
        self.page = 1;
      }
      this.putpage = page;
      axios({
        method: 'post',
        url: '/admin/admin/adminList',
        data: {
          page,
          search: self.search,
        },
      })
        .then((response) => {
          self.$store.commit('adminlistinfo', response.data);
        })
        .catch((response) => {
          console.log(response);
        });
      this.$router.push({
        path: '/admin', // 这是路由名称
        query: { page, search: self.search },
      });
    },
    // 控制全选，全不选
    handleSelectAll(status) {
      this.$refs.selection.selectAll(status);
    },
    // 添加用户跳页
    addAdmin() {
      this.$router.push({
        path: '/adminupdate', // 这是路由名称
      });
    },
    /**
     * 编辑当前用户
     * localStorage html5新定义的属性，存在兼容问题  使用方法参考下边网址
     * https://www.cnblogs.com/st-leslie/p/5617130.html
     * https://segmentfault.com/q/1010000010175869?sort=created
     */
    show(index) {
      // if(!window.localStorage){
      //     alert("浏览器不支持localstorage");
      // }else{
      // var storage = window.localStorage;//实例化localStorage，
      // localStorage.userid = index;//将用户id存入localStorage
      // 用query方法传值，不用有导航守卫带值
      this.$router.push({
        path: '/adminupdate', // 这是路由名称
        query: { userid: index },
      });
      // }
    },
    /**
     * 删除当前用户
     * 同页面刷新不建议使用路由，再此请求数据
     * const定义的变量不可修改
     */
    remove(param) {
      this.modal1 = true;
      this.param = param;
    },
    ok() {
      const self = this;// 将this对象附到一个不可更改的变量中~
      axios({
        method: 'post',
        url: '/admin/admin/adminDel',
        data: { id: self.param.row.id },
      })
        .then((response) => {
          checkRule(response, self);// 检查是否有权限

          if (response.data.code == 1) {
            self.data6.splice(self.param.index, 1);// 如果把那个1改成0，就是不删除
            if (self.param.index == 0 && self.putpage - 1 >= 1) {
              self.$store.commit('admininfoagain', self.putpage - 1, self.search);
            } else {
              self.$store.commit('admininfoagain', self.putpage, self.search);
            }
            self.$Message.success(response.data.msg);
            // self.data6.splice(param.index, 1);//如果把那个1改成0，就是不删除
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {
          self.$Message.error(response.data.msg);
        });
    },
    cancel() {
      this.modal1 = false;
    },
  },
};

